package myjava.test.ui.model;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class FreeFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public boolean leftTop;

	public boolean top;

	public boolean rightTop;

	public boolean leftCentre;

	public boolean centre;

	public boolean rightCentre;

	public boolean leftButtom;

	public boolean buttom;

	public boolean rightButtom;

	public int maxWidthSize = 10000;
	
	public int maxHeightSize = 10000;
	
	public int minWidthSize = 2;

	public int minHeightSize = 2;

	public int Border = 5;

	public static void main(String[] args) {
		FreeFrame frame = new FreeFrame(500, 200, 300, 500);
		frame.setMaxHeightSize(600);
		frame.setMaxWidthSize(600);
		frame.setMinHeightSize(200);
		frame.setMinWidthSize(200);
		frame.setBackground(Color.gray);
		frame.setVisible(true);
	}

	public FreeFrame() {
		//TODO 让面板居中
		new FreeFrame(200,200,200,200);
	}

	public FreeFrame(int x, int y, int width, int height) {
		this.setSize(width, height);
		this.setLocation(x, y);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setUndecorated(true);
		this.setLayout(new BorderLayout());
		this.setBackground(new Color(0, 0, 0, 0));
		onListen();
	}

	public void setMaxWidthSize(int maxWidthSize) {
		this.maxWidthSize = maxWidthSize;
	}

	public void setMaxHeightSize(int maxHeightSize) {
		this.maxHeightSize = maxHeightSize;
	}

	public void setMinWidthSize(int minWidthSize) {
		this.minWidthSize = minWidthSize;
	}

	public void setMinHeightSize(int minHeightSize) {
		this.minHeightSize = minHeightSize;
	}

	public void onListen() {
		this.getContentPane().addMouseMotionListener(new MouseAdapter() {
			
			int nowX;
			
			int nowY;
			
			@Override
			public void mouseMoved(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				nowX = x;
				nowY = y;
				int width = getWidth();
				int height = getHeight();
				int cursorType = Cursor.DEFAULT_CURSOR;
				top=leftTop = buttom = centre = leftButtom = leftCentre = leftTop = rightButtom = rightCentre = rightTop = false;
				if (x - Border < 0) {
					if (y - Border < 0) {
						cursorType = Cursor.NW_RESIZE_CURSOR;
						leftTop = true;
					} else if (y + Border >= height) {
						cursorType = Cursor.SW_RESIZE_CURSOR;
						leftButtom = true;
					} else {
						cursorType = Cursor.W_RESIZE_CURSOR;
						leftCentre = true;
					}
				} else if (x + Border >= width) {
					if (y - Border < 0) {
						cursorType = Cursor.SW_RESIZE_CURSOR;
						rightTop = true;
					} else if (y + Border >= height) {
						cursorType = Cursor.NW_RESIZE_CURSOR;
						rightButtom = true;
					} else {
						cursorType = Cursor.W_RESIZE_CURSOR;
						rightCentre = true;
					}
				} else {
					if (y - Border < 0) {
						cursorType = Cursor.S_RESIZE_CURSOR;
						top = true;
					} else if (y + Border >= height) {
						cursorType = Cursor.N_RESIZE_CURSOR;
						buttom = true;
					} else{
						centre = true;
					}
				}
				setCursor(new Cursor(cursorType));
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				int x = e.getX();
				int y = e.getY();
				int width = getWidth();
				int height = getHeight();
				if (leftTop) {
					if(width - x > maxWidthSize || width - x < minWidthSize) x = 0;
					if(height - y > maxHeightSize || height - y < minHeightSize) y = 0;
					setLocation(getX() + x, getY() + y);
					setSize(width - x, height - y);
				}
				if(leftButtom){
					if(width - x > maxWidthSize || width - x < minWidthSize) x = 0;
					if( y > maxHeightSize || y < minHeightSize) y = height;
					setLocation(getX() + x, getY());
					setSize(width - x, y);
				}
				if(rightTop){
					if( x > maxWidthSize ||  x < minWidthSize) x = width;
					if(height - y > maxHeightSize || height - y < minHeightSize) y = 0;
					setLocation(getX(), getY() + y);
					setSize(x, height - y);
				}
				if(rightButtom){
					if( x > maxWidthSize ||  x < minWidthSize) x = width;
					if(y > maxHeightSize ||  y < minHeightSize) y = height;
					setSize(x, y);
				}
				if(leftCentre) {
					if(width - x > maxWidthSize || width - x < minWidthSize) x = 0;
					setLocation(getX() + x, getY());
					setSize(width - x, height);
				}
				if(top){
					if(height - y > maxHeightSize || height - y < minHeightSize) y = 0;
					setLocation(getX(), getY() + y);
					setSize(width, height - y);
				}
				if(buttom){
					if(y > maxHeightSize || y < minHeightSize) y = height;
					setSize(width, y);
				}
				if(rightCentre){
					if( x > maxWidthSize ||  x < minWidthSize) x = width;
					setSize(x, height);
				}
				if(centre){
					setLocation(getX() + x - nowX, getY() + y - nowY);
				}
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				System.out.println("鼠标离开了");
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				System.out.println("鼠标进入了");
			}

		});
	}

}
